package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.adapter.CRMMainAdapter;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.widget.DividerGridView;

/**
 * Created by Megan on 16/7/27.
 */
public class CRMFragment extends Fragment implements View.OnClickListener {

    private View rootView, lineView;
    private LinearLayout mLlBack;
    private TextView mTvTitle;
    private DividerGridView mGridView;
    private CRMMainAdapter mAdapter;
    private MainActivity mActivity;
    private String mTextArray[] = {"公海库", "销售线索", "销售机会", "客户", "报表统计", "外勤签到", "管理"};
    private int mImageArray[] = {R.drawable.crm_sea,
            R.drawable.crm_clue, R.drawable.crm_chance, R.drawable.crm_client,
            R.drawable.crm_report, R.drawable.crm_legwork_sign, R.drawable.crm_manager};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.crm_frag_layout, container, false);
            initView(rootView);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    private void initView(View view) {
        mLlBack = (LinearLayout) view.findViewById(R.id.title_ll_back);
        mTvTitle = (TextView) view.findViewById(R.id.title_tv_center);
        lineView = (View) view.findViewById(R.id.title_v_line);
        mGridView = (DividerGridView) view.findViewById(R.id.gv_crm_main);

        mLlBack.setVisibility(View.GONE);
        mTvTitle.setText("CRM");
        lineView.setVisibility(View.GONE);
        mAdapter = new CRMMainAdapter(mActivity, mTextArray, mImageArray);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(mActivity,CRMLibraryActivity.class);
                switch (position) {
                    case 0:
                        intent.putExtra("type", Constants.CRM_PUBLIC_CLUE);
                        startActivity(intent);
                        break;
                    case 1:
                        intent.putExtra("type", Constants.CRM_PRIVATE_CLUE);
                        startActivity(intent);
                        break;
                    case 2:
                        intent.putExtra("type", Constants.CRM_CHANCE);
                        startActivity(intent);
                        break;
                    case 3:
                        intent.putExtra("type", Constants.CRM_CLIENT);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
